package com.woniu.account.controller;


import com.woniu.account.dto.*;
import com.woniu.account.form.*;
import com.woniu.account.model.TUserInfo;
import com.woniu.account.service.TUserInfoService;
import com.woniu.core.util.JwtUtil;
import com.woniu.core.util.PageInfo;
import com.woniu.core.util.RedisUtil;
import com.woniu.core.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年04月02日
 */
@RestController
@RequestMapping("/tUserInfo")
@Api(tags = "用户表相关接口")
public class TUserInfoController {

    @Resource
    private TUserInfoService tUserInfoService;
    @Resource
    private RedisUtil redisUtil;
    @ApiOperation("根据id查询用户信息")
    @GetMapping("/getUserInfoById/{id}.api")
    public Result<GetUserInfoByIdDTO> getUserInfoById(@PathVariable("id") Integer id) throws Exception{
        GetUserInfoByIdDTO dto = tUserInfoService.getUserInfoById(id);
        return Result.getResult(GetUserInfoByIdDTO.class).setMsg("查询成功").setData(dto);
    }

    @ApiOperation("用户账号注册")
    @PostMapping("/userRegister.user")
    public Result userRegister(@RequestBody @Valid RegisterUserInfoForm form) throws Exception{
        TUserInfo tUserInfo=tUserInfoService.userRegister(form);
        return Result.getResult(TUserInfo.class).setMsg("注册成功").setData(tUserInfo);
    }

    @PostMapping("/sendRegisterCode")
    @ApiOperation("用户发送注册短信验证码")
    public Result sendRegisterCode(String phone) throws Exception{
        tUserInfoService.sendRegisterCode(phone);
        return Result.getResult(Boolean.class).setMsg("发送验证码成功").setData(true);
    }

    @ApiOperation("用户账号密码登录")
    @PostMapping("/userLogin.user")
    public Result userLogin(@RequestBody @Valid LoginUserInfoForm form) throws Exception{
        UserLoginDTO userCoachLoginDTO = tUserInfoService.userLogin(form);
        return Result.getResult(UserLoginDTO.class).setMsg("登录成功").setData(userCoachLoginDTO);
    }

    @PostMapping("/sendLoginCode/{phone}")
    @ApiOperation("用户发送登录验证码")
    //根据你的token里面带的角色动态生成菜单
    public Result sendLoginCode(@PathVariable String phone) throws Exception{
        tUserInfoService.sendLoginCode(phone);
        return Result.getResult(Boolean.class).setMsg("发送成功").setData(true);
    }

    @ApiOperation("用户手机验证码登录")
    @PostMapping("/userCodeLogin.user")
    public Result coachCodeLogin(@RequestBody @Valid CoachCodeLoginForm form) throws Exception{
        UserLoginDTO userCoachLoginDTO = tUserInfoService.coachCodeLogin(form);
        return Result.getResult(UserLoginDTO.class).setMsg("登录成功").setData(userCoachLoginDTO);
    }


    @ApiOperation("用户修改密码")
    @PostMapping("/userChangePassword.user")
    public Result userChangePassword(@RequestBody @Valid ChangePasswordForm form) throws Exception{
        System.out.println(form.getAccountPass());
        System.out.println(form.getNewAccountPass());
        System.out.println(form.getNewAccountPass1());
        tUserInfoService.userChangePassword(form);
        return Result.getResult(Boolean.class).setMsg("修改成功").setData(true);
    }

    @ApiOperation("用户信息完善")
    @PostMapping("/perfectInformation.user")
    //和教练共用一个form，前端接收数据时使用
    public Result perfectInformation(@RequestBody @Valid updateUserInformationForm form) throws Exception{
        UserInfoChangeDto tUserInfo=tUserInfoService.perfectInformation(form);
        return Result.getResult(UserInfoChangeDto.class).setMsg("修改成功").setData(tUserInfo);
    }

    @ApiOperation("用户实名认证")
    @PostMapping("/realNameAuthentication.user")
    public Result userRealNameAuthentication(@RequestBody RealNameAuthenticationForm form) throws Exception{
        String str = tUserInfoService.userRealNameAuthentication(form);
        return Result.getResult(String.class).setMsg(str);
    }

    @ApiOperation("用户查询关注的教练")
    @GetMapping("/myFollowCoach")
    public Result myFollowCoach(Integer pageNum,Integer pageSize,@RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        Integer userId = Integer.valueOf(JwtUtil.parseToken(refreshToken,2).get("id").toString());
        PageInfo<MyFollowCoachDto> dto = tUserInfoService.myFans(pageNum,pageSize,userId);
        return Result.getResult(PageInfo.class).setMsg("查询成功").setData(dto);
    }

    @PostMapping("/uploadImage.user")
    @ApiOperation("用户修改图片")
    //如果参数是MultipartFile来传参，就要分开；如果用String在参数对象就可以用参数对象而且要用@requestBody注解
    public Result uploadImage(MultipartFile image) throws Exception {
        Integer userId = JwtUtil.getUserId(2);
        UpdateImageForm form = new UpdateImageForm();
        form.setImage(image);
        form.setAccountId(userId);
        String s = tUserInfoService.uploadImage(form);
        return Result.getResult(String.class).setMsg("更改成功").setData(s);
    }
}

